import { ref } from 'vue';
import api from '../api/api';

// 获取分类列表
export const getCategories = async () => {
  try {
    const response = await api.get('/catg')
    if (response.data.code === 1 && response.data.msg === 'success') {
      return response.data.data
    }
  } catch (err) {
    console.error('获取分类失败:', err)
  }
  return []
}
export const useCategories = () => {
  const categoriesMap = ref({});
  const fetchCategories = async () => {
    try {
      const data = await getCategories();
      const map = {};
      data.forEach(cat => {
        map[cat.id] = cat.name;
      });
      categoriesMap.value = map;
    } catch (err) {
      console.error('获取分类失败:', err);
    }
  };
  return { categoriesMap, fetchCategories, getCategories };
};